from datetime import datetime
from public.Logs.log import log1
from public.conifig import connect


# 数据保存
def save_data(cursor, datas):
    create_by = 'spider'
    type = 1
    status = 1
    readnumber = 0
    deleted = 0
    sql = "insert into tax_article(title, source_name, source_id, source_url, article_date, content, source_name_id, category_id, sub_title, author, image, create_by, type, status, readnumber, deleted) values({},'{}','{}','{}','{}',{},'{}','{}',{},'{}','{}',{},{},{},{},{})".format(datas[0], datas[1], datas[2], datas[3], datas[4], datas[5], datas[6], datas[7], datas[8], datas[9], datas[10], create_by, type, status, readnumber, deleted)
    # cursor = connect.cursor()
    try:
        cursor.execute(sql)
        connect.commit()
        log1.info("保存成功({})".format(datas[0]))
    except Exception as e:
        # print("保存失败({}):{}".format(datas[0], e))
        log1.error("保存失败({}):{}".format(datas[0], e))
    # cursor.close()


# 查询数据库最晚文章日期
def max_date(cursor, source_name_id):
    # cursor = connect.cursor()
    sql = "select max(article_date) from tax_article where source_name_id={}".format(source_name_id)
    cursor.execute(sql)
    sql_result = cursor.fetchone()
    max_date = sql_result[0] if sql_result else ""
    # cursor.close()
    return max_date